<?php

if_get('/static/*.png', function ()
{/*{{{*/
    $path = ROOT_DIR.'/public/static/404.png';
    header('Content-Type:image/png;');
    echo file_get_contents($path);
    exit();
});/*}}}*/

if_get('/', function ()
{/*{{{*/
    $account_info = get_portal_account_info();

    $total_volume = get_24h_volume_cny_ratio();

    $total_volume_sort_by_ratio = $total_volume_sort_by_diff = $total_volume;

    uasort($total_volume_sort_by_ratio, function ($a, $b) {
        if ($a['24h_volume_cny'] == $b['24h_volume_cny']) {
            if ($a['volume_rank_rank'] == $b['volume_rank_rank']) {
                return 0;
            } else {
                return ($a['volume_rank_rank'] < $b['volume_rank_rank']) ? 1 : -1;
            }
        }
        return ($a['24h_volume_cny'] < $b['24h_volume_cny']) ? 1 : -1;
    });

    uasort($total_volume_sort_by_diff, function ($a, $b) {
        if ($a['volume_rank_rank'] == $b['volume_rank_rank']) {
            if ($a['24h_volume_cny'] == $b['24h_volume_cny']) {
                return 0;
            } else {
                return ($a['24h_volume_cny'] < $b['24h_volume_cny']) ? 1 : -1;
            }
        }
        return ($a['volume_rank_rank'] < $b['volume_rank_rank']) ? 1 : -1;
    });

    $total_volume_sort_by_diff = array_values($total_volume_sort_by_diff);

    return render('index/index', [
        'account_info' => $account_info,
        'last_record_time' => get_last_record_time(),
        'new_coin_markets' => get_new_coinmarket(10),
        'total_volume_sort_by_ratio' => $total_volume_sort_by_ratio,
        'total_volume_sort_by_diff' => $total_volume_sort_by_diff,
        'percent_change_1h_asc' => get_percent_change_1h(50, 'ASC'),
        'percent_change_1h_desc' => get_percent_change_1h(50, 'DESC'),
        'percent_change_24h_asc' => get_percent_change_24h(50, 'ASC'),
        'percent_change_24h_desc' => get_percent_change_24h(50, 'DESC'),
        'rank_daily_rise_desc' => get_last_rank_daily_rise('DESC'),
        'rank_daily_rise_asc' => get_last_rank_daily_rise('ASC'),
        'rank_weekly_rise_desc' => get_last_rank_weekly_rise('DESC'),
        'rank_weekly_rise_asc' => get_last_rank_weekly_rise('ASC'),
    ]);
});/*}}}*/

if_get('/analysis_crawler', function ()
{/*{{{*/
    $account_info = get_portal_account_info();

    $start_time = input('start_time', datetime('-1 week', 'Y/m/d'));
    $end_time = input('end_time', datetime('now', 'Y/m/d'));

    $start_time_query = datetime($start_time.' 00:00:00');
    $end_time_query = datetime($end_time.' 23:59:59');

    $time_line_data = get_crawler_abnormal_time_line_infos($start_time_query, $end_time_query);

    $abnormal_sum_distributed_infos = get_crawler_abnormal_sum_key_distributed_infos($start_time_query, $end_time_query);

    $abnormal_count_distributed_infos = get_crawler_abnormal_count_key_distributed_infos($start_time_query, $end_time_query);

    $tmp_abnormal_time_distributed_infos = get_crawler_abnormal_count_time_distributed_infos($start_time_query, $end_time_query);

    $tmp_abnormal_time_distributed_infos_2 = [];

    foreach ($tmp_abnormal_time_distributed_infos as $abnormal_time_distributed_info) {

        $w = (int) datetime($abnormal_time_distributed_info['time'].':00:00', 'w');
        $h = (int) datetime($abnormal_time_distributed_info['time'].':00:00', 'H');

        if (! isset($tmp_abnormal_time_distributed_infos_2[$w])) {
            $tmp_abnormal_time_distributed_infos_2[$w] = [];
        }

        if (! isset($tmp_abnormal_time_distributed_infos_2[$w][$h])) {
            $tmp_abnormal_time_distributed_infos_2[$w][$h] = 0;
        }

        $tmp_abnormal_time_distributed_infos_2[$w][$h] += $abnormal_time_distributed_info['count'];
    }

    $abnormal_time_distributed_max_count = 0;
    $abnormal_time_distributed_infos = [];
    foreach ($tmp_abnormal_time_distributed_infos_2 as $w => $h_infos) {
        foreach ($h_infos as $h => $count) {
            $abnormal_time_distributed_infos[] = [$w, $h, $count];
            $abnormal_time_distributed_max_count = max($abnormal_time_distributed_max_count, $count);
        }
    }

    return render('index/analysis_crawler', [
    
        'start_time' => $start_time,
        'end_time' => $end_time,
        'abnormal_time_distributed_max_count' => $abnormal_time_distributed_max_count,

        'time_line_data' => array_build($time_line_data, function ($key, $value) {
            return [$value['abnormal_date'], [
                'abnormal_count' => $value['abnormal_count'],
                'abnormal_sum' => $value['abnormal_sum'],
            ]];
        }),

        'abnormal_sum_distributed_infos' => array_reverse(array_build($abnormal_sum_distributed_infos, function ($key, $value) {
            return [$value['key'], $value['abnormal_sum']];
        })),

        'abnormal_count_distributed_infos' => array_reverse(array_build($abnormal_count_distributed_infos, function ($key, $value) {
            return [$value['key'], $value['abnormal_count']];
        })),

        'abnormal_time_distributed_infos' => $abnormal_time_distributed_infos,

    ]);
});/*}}}*/

if_get('/mark_crawler', function ()
{/*{{{*/
    $account_info = get_portal_account_info();

    $abnormal_values = get_crawler_abnormal_values();

    return render('index/mark_crawler', [
        'abnormal_values' => $abnormal_values,
    ]);
});/*}}}*/

if_post('/ajax/mark_crawler', function ()
{/*{{{*/
    $account_info = get_portal_account_info();

    list($action, $abnormal_id) = input_list('action', 'abnormal_id');

    otherwise(isset(CRAWLER_BANKER_ACTION_MAPS[$action]), '无效的 action');

    db_simple_update('crawler_coin_abnormal_volume', [
        'id' => $abnormal_id,
    ], [
        'banker_action' => $action,
    ]);
});/*}}}*/

if_get('/watch_crawler', function ()
{/*{{{*/
    //$account_info = get_portal_account_info();
    //
    $start_time_infos = [
        '-15 days' => '15 天内',
        '-30 days 00:00:00' => '30 天前',
        '-45 days 00:00:00' => '45 天前',
        '-60 days 00:00:00' => '60 天前',
        '-75 days 00:00:00' => '75 天前',
        '-90 days 00:00:00' => '90 天前',
        '-105 days 00:00:00' => '105 天前',
        '-120 days 00:00:00' => '120 天前',
        '-135 days 00:00:00' => '135 天前',
        '-150 days 00:00:00' => '150 天前',
        '-165 days 00:00:00' => '167 天前',
        '-180 days 00:00:00' => '180 天前',
        '-195 days 00:00:00' => '195 天前',
        '-210 days 00:00:00' => '210 天前',
    ];

    $symbol = input('symbol', '');
    $start = input('start', key($start_time_infos));

    $abnormal_infos = $binance_infos = [];

    $abnormal_symbol_enum = get_crawler_abnormal_symbol_enum();

    if ($symbol) {

        $start_time = datetime($start);

        $end_time = datetime($start_time.' +15 days');

        $tmp_abnormal_infos = get_crawler_abnormal_values_by_symbol($symbol, $start_time, $end_time);

        $binance_infos = [];

        $start_time_long = strtotime($start_time) * 1000;

        while (($diff = (int) (datetime_diff(datetime($start_time_long / 1000), $end_time, '%r%tm') / 5)) > 0) {

            $limit = min($diff, 1000);

            $tmp_binance_infos = remote_get_json('https://api.binance.com/api/v1/klines?symbol='.$symbol.'&interval=5m&startTime='.$start_time_long.'&limit='.$limit, 3, 10);

            $tmp_end_infos = end($tmp_binance_infos);

            $start_time_long = $tmp_end_infos[6];

            $binance_infos = array_merge($binance_infos, $tmp_binance_infos);
        }

        $abnormal_infos = array_build($tmp_abnormal_infos, function ($k, $v) {

            $time = datetime($v['volume_at'], 'Y/m/d H:i');

            $v['volume_at_short'] = $time;

            return [$time, $v];
        });
    }

    return render('index/watch_crawler', [

        'binance_infos' => array_build($binance_infos, function ($k, $v) use (&$abnormal_infos) {

            $time = datetime($v[6] / 1000, 'Y/m/d H:i');

            if (isset($abnormal_infos[$time])) {

                $abnormal_infos[$time]['price'] = $v[4];
            }

            return [null, [
                $time,
                $v[1],
                $v[4],
                $v[3],
                $v[2]
            ]];
        }),

        'abnormal_data' => array_values($abnormal_infos),
        'abnormal_symbol_enum' => $abnormal_symbol_enum,
        'symbol' => $symbol,
        'start' => $start,
        'start_time_infos' => $start_time_infos,
    ]);
});/*}}}*/
